package com.yaolong.api.modules.opms.service.impl;

import com.yaolong.api.modules.opms.entity.Message;
import com.yaolong.api.modules.opms.dao.MessageDao;
import com.yaolong.api.modules.opms.service.MessageService;
import com.yaolong.api.modules.sys.dto.UserInfoDto;
import com.yaolong.api.modules.sys.po.RoleInfo;
import com.yaolong.api.modules.sys.service.ISysUserInfoService;
import com.yaolong.api.modules.utils.CheckRoleUtils;
import com.yaolong.commons.base2.BaseServiceImpl;
import com.yaolong.commons.utils.MapperUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * <p>
 * 消息通知 服务实现类
 * </p>
 *
 * @author yaolonga
 * @version v1.0.0
 * @since 2023-02-05
 */
@Service
public class MessageServiceImpl extends BaseServiceImpl<MessageDao, Message> implements MessageService {
    @Autowired
    private ISysUserInfoService iSysUserInfoService;

    @Override
    public IPage<Message> page(int current, int size, Message domain) {
        IPage<Message> page = new Page<>(current, size);
        //根据角色获取家庭id信息
        UserInfoDto userInfoDto = iSysUserInfoService.userInfoByContext();
        List<RoleInfo> roleInfos = MapperUtils.copyList(userInfoDto.getRoles(), RoleInfo.class);
        Long userId = userInfoDto.getUserId();
        boolean flag = CheckRoleUtils.checkRole(roleInfos);
        //居民
        if (!flag) {
            domain.setSenderBy(userId);
            domain.setReceiverBy(userId);
        }

        return lambdaQuery()
                .eq(!ObjectUtils.isEmpty(domain.getIsHandle()), Message::getIsHandle, domain.getIsHandle())
                .eq(!ObjectUtils.isEmpty(domain.getSenderBy()), Message::getSenderBy, domain.getSenderBy())
                .or()
                .eq(!ObjectUtils.isEmpty(domain.getReceiverBy()), Message::getReceiverBy, domain.getReceiverBy())
                .like(!ObjectUtils.isEmpty(domain.getReceiverByName()), Message::getReceiverByName, domain.getReceiverByName())
                .like(!ObjectUtils.isEmpty(domain.getSenderByName()), Message::getSenderByName, domain.getSenderByName())
                .page(page);
    }

    @Override
    public List<Message> list(Message domain) {
        return lambdaQuery()
                .like(!ObjectUtils.isEmpty(domain.getId()), Message::getId, domain.getId())
                .like(!ObjectUtils.isEmpty(domain.getReceiverBy()), Message::getReceiverBy, domain.getReceiverBy())
                .like(!ObjectUtils.isEmpty(domain.getReceiverByName()), Message::getReceiverByName, domain.getReceiverByName())
                .list();
    }


}
